<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/28
 * Time: 10:14
 */

namespace PayCenter;
use Common\Model\LogModel;
use Common\Model\UtilsModel;
use Common\Model\WeixinModel;
use Think\Controller;
use Common\Model\UserModel;
class BaseController extends Controller {

    protected $smarty=null;
    protected $isWechat=0;
    public function _empty(){
        $this->callMethod();
    }


    //城市魔方
    private function wxLogin_csmf(){

        $openid     = isset($_REQUEST['openid']) ? $_REQUEST['openid'] : null;
        $redirect   = session('pay_redirect') ? session('pay_redirect') : BASE_DOMAIN.'pay/paycenter.php?m=paycenter';

        $gourl =  urlencode($redirect);

        header("Content-Type: text/html;charset=utf-8");
        $string = urldecode($_SERVER['QUERY_STRING']);
        $ret = array();
        @parse_str($string, $ret);

        //若获取不到openid
        if (!$ret['openid']) {
            $url = PAY_WECHAT_AUTHORIZATION_URI."backurl=" . urlencode($gourl);
            header("Location:" . $url);
            die;
        }


        //复制获取openid
        session('pay_openid',$ret['openid']);
        session('pay_unionid',$ret['unionid']);

        $redirect = session('pay_redirect');
        session('pay_redirect',null);
        session('pay_starLogin',1);
        header("Location: ".$redirect);
        die;
    }
    private function wxLogin_czk(){
        $openid     = isset($_REQUEST['openid']) ? $_REQUEST['openid'] : null;
        $redirect   = session('pay_redirect') ? session('pay_redirect') : BASE_DOMAIN.'wap.php?m=Wap';
        $gourl      = urlencode($redirect);
        die($gourl);
        header("Content-Type: text/html;charset=utf-8");

        //若获取不到openid
        if (!$_GET['ui']) {
            $url = PAY_WECHAT_AUTHORIZATION_URI."bu=" . urlencode($gourl);
            header("Location:" . $url);
            die;
        }

        $ret=json_decode(urldecode($_GET['ui']),1);
        session('pay_starLogin',1);
        //复制获取openid
        session('pay_openid',$ret['open_id']);

        $redirect = session('pay_redirect');
        session('pay_redirect',null);
        header("Location: ".$redirect);
        die;
    }
    private function wxLogin_chensu(){
        $openid     = isset($_REQUEST['openid']) ? $_REQUEST['openid'] : null;
        $redirect   = session('pay_redirect') ? session('pay_redirect'): BASE_DOMAIN.'wap.php?';
        $gourl =  urlencode($redirect);
        header("Content-Type: text/html;charset=utf-8");
        $string = urldecode($_SERVER['QUERY_STRING']);
        $ret = array();
        @parse_str($string, $ret);

        //若获取不到openid
        if (!$ret['openid']) {
            $url = PAY_WECHAT_AUTHORIZATION_URI."backurl=" . urlencode($gourl);
            header("Location:" . $url);
            die;
        }

        //复制获取openid
        session('pay_openid',$ret['openid']);
        session('pay_unionid',$ret['unionid']);

        $redirect = session('pay_redirect');
        session('pay_redirect',null);
        session('pay_starLogin',1);
        header("Location: ".$redirect);
        die;
    }
    function _initialize(){
        UtilsModel::checkMaintenance();

        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $this->isWechat = 0;

        if(strpos($user_agent, 'MicroMessenger') !== false ) {
            UtilsModel::disableClientAndTip(ENABLE_WEIXIN);
            session('pay_redirect', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
            $this->isWechat = 1;


            if(!session('pay_openid') || !session('pay_starLogin')) {
                switch (CURRENT_PAY){
                    case COMPANY_CSMF:
                        $this->wxLogin_csmf();
                    case COMPANY_KNB:
                        $this->error("丸子妈妈是订阅号，不可支付");
                        break;
                    case COMPANY_WZMA:
                        $this->wxLogin_chensu();
                        break;
                }
            }
        }
        else{
            UtilsModel::disableClientAndTip(ENABLE_WAP);
        }


        $this->smarty=getSmarty();


        //微信分享
        $this->smarty->assign('isWechat',$this->isWechat);
        if($this->isWechat){
            
            $wxReturn = WeixinModel::getSignPackage(CURRENT_PAY);

            if($wxReturn['status']==0){
                $this->smarty->assign('wxSign',$wxReturn['data']);
            }
        }
    }



    /**
     * 重置session的参数
     * User: Rick.Sun.<sunwgjj@126.com>
     * Return: void
     * Address: 浙江宣逸网络科技有限公司
     * @param $data
     */
    function resetUserSessions($data){
        foreach ($data as $k => $v) {
            session($k, $v);
        }
    }

    public function callMethod(){
        $method=ACTION_NAME.'_'.CURRENT_COMPANY;
        if(!method_exists($this,$method )){
            $this->error('未定义的方法:' . $method);
        }

        $ref_class = new \ReflectionClass($this);
        $instance  = $ref_class->newInstance();
        $method = $ref_class->getmethod($method);
        $method->setAccessible(true);
        $method->invoke($instance);
    }


}